/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

Ext.namespace('Serum.Form.Module.Configurations', 'Serum.Grid.Module.Configurations');

Serum.Module.Configurations = Ext.extend(Serum.Module.Base, {

    formClass: function() {
        switch(Serum.Selector.get().action) {
            case 'edit': return 'Serum.Form.Module.Configurations.Edit';
            default: return null;
        }
    },

    listClass: function() {
        return 'Serum.Grid.Module.Configurations.List';
    }

});

Serum.Grid.Module.Configurations.List = Ext.extend(Serum.Grid.Module.List, {

    constructor: function() {
        Serum.Grid.Module.Configurations.List.superclass.constructor.call(this);
    },

    /**
     * Returns the list of buttons of the toolbar
     */
    getToolbarButtons: function() {
        var toolbar = new Array();

        if ((typeof Serum.Selector.get().getParams['parentRoot'] !== 'undefined')) {
            toolbar.push(new Serum.Grid.Module.Toolbar.LevelUpButton(this));
        }

        return toolbar;
    },

    defaultSort: {sort: 'constantKey|id', dir: 'ASC|ASC'},

    /**
     * Returns columns list to display
     *
     * @return      {Array}
     */
    getColumns: function() {
        return [this.selectionModel(), {
            id:         'icon',
            header:     __('base~list_column_icon'),
            dataIndex:  'iconCls',
            renderer:   this.iconRenderer,
            width:      20
        }, {
            id:         'title',
            header:     __('configurations~list_key'),
            dataIndex:  'constantKey',
            renderer:   this.titleRenderer,
            sortable:   true
        }, {
            id:         'value',
            header:     __('configurations~list_value'),
            dataIndex:  'constantValue',
            sortable:   true,
            width:      200
        }];
    },

    /**
     * Extra rows
     */
    extraFields: ['constantKey', 'constantValue']
});

Serum.Form.Module.Configurations.Edit = Ext.extend(Serum.Form.Module.Edit, {

    fieldsList: function(results) {
        var constantValueConfig = {};

        if (results['default']['type']['value'] === 'boolean') {
            constantValueConfig['checked']   = (results['default']['constantValue']['value'] == 1);
            constantValueConfig['xtype']     = 'xcheckbox';

        } else if (results['default']['type']['value'] === 'number') {
            constantValueConfig['xtype'] = 'numberfield';
            constantValueConfig['value'] = results['default']['constantValue']['value'];

        } else {
            constantValueConfig['xtype'] = 'textfield';
            constantValueConfig['value'] = results['default']['constantValue']['value'];
        }

        return {
            'default': [
                {   'constantKey':     {},
                    'constantValue':   constantValueConfig,
                    'DbModule':        {}}
            ]
        };
    }
});
